package com.derive.education_service.web.platform.controller;

import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Business;
import com.derive.education_service.web.platform.bean.dto.BusinessAdd;
import com.derive.education_service.web.platform.bean.dto.BusinessUp;
import com.derive.education_service.web.platform.service.BusinessService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 机构主营业务应用接口
 */
@RestController
@RequestMapping("business")
@Validated
@Slf4j
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 新增机构主营业务数据
     *
     * @param file
     * @param businessAdd
     * @return
     */
    @PostMapping("addBusiness")
    public ResponseMessage addBusiness(@RequestPart("file") MultipartFile file, @Validated @RequestPart("businessAdd") BusinessAdd businessAdd) {
        return businessService.addBusiness(file, businessAdd);
    }

    /**
     * 删除机构主营业务数据 -- 根据id
     *
     * @param businessId
     * @return
     */
    @PostMapping("delBusiness")
    public ResponseMessage delBusiness(Integer businessId) {
        boolean flag = businessService.delBusiness(businessId);
        return flag ? ResponseMessage.createBySuccessMessage("删除成功") : ResponseMessage.createByErrorMessage("异常");
    }

    /**
     * 删除机构下所有的主营业务数据 -- 机构id
     *
     * @param platformId
     * @return
     */
    @PostMapping("batchDelBusiness")
    public ResponseMessage batchDelBusiness(Integer platformId) {
        boolean flag = businessService.batchDelBusiness(platformId);
        return flag ? ResponseMessage.createBySuccessMessage("批量删除成功") : ResponseMessage.createByErrorMessage("异常");
    }

    /**
     * 编辑机构主营业务数据
     *
     * @param file
     * @param businessUp
     * @return
     */
    @PostMapping("upBusiness")
    public ResponseMessage upBusiness(@RequestPart("file") MultipartFile file, @Validated @RequestPart("businessUp") BusinessUp businessUp) {
        return businessService.upBusiness(file, businessUp);
    }

    /**
     * 获取指定机构下的主营业务
     *
     * @param platformId
     * @return
     */
    @GetMapping("getBusinessListByPlatformId")
    public ResponseMessage getBusinessListByPlatformId(Integer platformId) {
        List<Business> businessList = businessService.getBusinessListByPlatformId(platformId);
        return ResponseMessage.createBySuccess(businessList);
    }

    /**
     * 根据id查询指定业务信息
     *
     * @param businessId
     * @return
     */
    @GetMapping("getBusinessById")
    public ResponseMessage getBusinessById(Integer businessId) {
        Business business = businessService.getBusinessById(businessId);
        return business == null ? ResponseMessage.createByErrorMessage("业务信息不存在") : ResponseMessage.createBySuccess(business);
    }
}
